/*
 * parser_test.cpp
 *          -- test functionality of parser
 *
 * Author: Qiu Shuang
 * Date: Mar 2nd, 2011
 */
#include <stdio.h>
#include <list>

#include "MPEGFile.h"
#include "MPEGFrameData.h"
#include "MPEGGranule.h"

int frame_parsed = 0;

int main(){
    MPEGFile *file = new MPEGFile("apologize.mp3", true);
    FILE *raw = fopen("my_raw_data.db", "w+");
    char buffer[256];
    std::list<MPEGFrameData *> data = file->contents();

    std::list<MPEGFrameData *>::const_iterator itr = data.cbegin();
    std::list<MPEGFrameData *>::const_iterator itr_end = data.cend();

    while(itr != itr_end){
        MPEGFrameData *data = *itr;
        int granule;
        for(granule = 0; granule < GRANULE; granule++){
            MPEGGranule *g = data->getGranule(granule);
            int channel;
            for(channel = 0; channel < CHANNEL; channel++){
                ChannelDataLong *channel_data = g->getDeHuffmanSamples(channel);

                int subband;
                int subsubband;
                for(subband = 0; subband < SUBBAND_COUNTS; subband++){
                    for(subsubband = 0; subsubband < SUBBAND_SAMPLES; subsubband++){
                        sprintf(buffer, "%ld\t", (*channel_data)[subband][subsubband]);

                        fwrite(buffer, 1, strlen(buffer), raw);
                    }

                    fwrite("\n", 1, 1, raw);
                }
            }
        }
        itr++;
    }

    printf("frame parsed %d\n", frame_parsed);

    delete file;

    getc(stdin);
}